<?php

namespace Biz\model;

use Doraemon\pockets\datebase\BaseDbModel;

class ShopCommissionRate extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_shop';

    const _DATABASE_ = 'shop';

    const _TABLE_NAME = 'shop_commission_rate';

    const _SUB_TABLE = false;

    const STATUS_DISABLED = 0;//失效状态

    const STATUS_NORMAL = 1;//正常状态

    const TYPE_NORMAL = 0;//默认佣金比例

    const TYPE_CATEGORY = 1;//按类目佣金比例

    public static function getRatioList($uid)
    {
        return self::select('*', [
            'uid' => $uid
        ]);
    }

    public static function getRatioInfo($uid, $categoryId)
    {
        return self::get('*', [
            'uid' => $uid,
            'category_id' => $categoryId
        ]);
    }

    public static function getShopRatioList($uid, $categoryIds)
    {
        return self::select('*', [
            'uid' => $uid,
            'category_id' => $categoryIds
        ]);
    }

    public static function getRatioInfoByCategoryId($shopId, $categoryId)
    {
        return self::get('*', [
            'shop_id' => $shopId,
            'category_id' => $categoryId,
            'status' => self::STATUS_NORMAL
        ]);
    }

    public static function addRatio($data)
    {
        self::insert([
            "uid" => $data['uid'],
            "shop_id" => $data['shop_id'],
            "type" => $data['type'],
            "category_id" => $data['category_id'],
            "status" => $data['status'],
            'is_defined_ratio' => $data['is_defined_ratio'],
            'ratio' => $data['ratio']
        ]);
        return self::id();
    }

    /**
     * 设置默认佣金比例
     * @param $uid
     * @param $shopId
     * @param $ratio
     */
    public static function addDefinedRatio($uid, $shopId, $ratio)
    {
        $info = self::getRatioInfo($uid, 0);
        if (!$info) {
            self::insert([
                "uid" => $uid,
                "shop_id" => $shopId,
                "type" => self::TYPE_NORMAL,
                "category_id" => 0,
                "status" => self::STATUS_NORMAL,
                'is_defined_ratio' => 1,
                'ratio' => $ratio
            ]);
            return self::id();
        } else {
            $result = self::updateInfo(['ratio' => $ratio], ['uid' => $uid, 'shop_id' => $shopId, 'is_defined_ratio' => 1]);
            if ($result) {
                return $info['id'];
            } else {
                return false;
            }
        }
    }

    public static function updateInfo($data, $where)
    {
        $result = self::update($data, $where);
        return $result->rowCount();
    }


    /**
     * 判断给定的分类ID是否在支持的列表中
     */
    public static function checkSupportByCategoryIds($shopId, $categoryIds)
    {
        $res = self::select(['category_id'], ['shop_id' => $shopId, 'category_id' => $categoryIds, 'status' => self::STATUS_NORMAL]);

        if ($res) {
            return count($res) == count($categoryIds);
        }

        return false;
    }

    public static function getDefaultRatio($shopId)
    {
        $result = self::get(['ratio'], ['shop_id' => $shopId, 'type' => self::TYPE_NORMAL, 'status' => self::STATUS_NORMAL]);
        return $result ? $result['ratio'] : null;
    }
}